// i.r(t), i.d(t, { default: () => LocaleModule })
import Module from "./Module"
import Logger from "../utils/Logger"
import polyglot from "node-polyglot"
import langConst from "../constant/lang.const"
async function a(e, t) {
  const i = (function (e) {
      return e === langConst.defaultLang ? "./locale/strings.json" : `./locale/messages/strings_${e}.json`
    })(e),
    n = await t.get(i)
  return JSON.parse(n)
}
import SettingsModule from "./SettingsModule"
import browserUtil from "../utils/browser.util"
import { getLang } from "../utils/lang.util"
import funcUtil from "../utils/func.util"
const u = new Logger("Locale")
export default class LocaleModule extends Module {
  _languageCode: any
  polyglot: polyglot
  static missingPrefix: string

  constructor() {
    super(...arguments)
    this.name = "locale"
  }
  async init(e, t) {
    this._languageCode = (function () {
      const e = funcUtil.getUrlParams("lang", browserUtil.languageInfo())
      return getLang(e)
    })()
    const i = this._languageCode.substr(0, 2)
    this.polyglot = new polyglot({
      locale: i,
      interpolation: { prefix: "%{", suffix: "}" },
      onMissingKey: e => (u.warn("Missing phrase key:" + e), LocaleModule.missingPrefix + e)
    })
    const n = await (async function (e, t) {
      const i = await a(e, t)
      if (e === langConst.defaultLang) return i
      const n = await a(langConst.defaultLang, t)
      return Object.assign(Object.assign({}, n), i)
    })(this._languageCode, e.queue)
    this.polyglot.extend(n), this.registerSettings(t)
  }
  t(e: string, t?: number | polyglot.InterpolationOptions) {
    return this.polyglot.t(e, t)
  }
  get languageCode() {
    return this._languageCode
  }
  async registerSettings(e) {
    ;(await e.getModule(SettingsModule)).registerSetting(langConst.langKey, this.languageCode)
  }
}
LocaleModule.missingPrefix = "FIX-"
